var t = require("../../data/staticData.js"), e = require("../../utils/utils.js");

Page({
    data: {
        alternative: null,
        cities: [],
        showItems: null,
        inputText: "",
        hotCities: []
    },
    cancel: function() {
        this.setData({
            inputText: "",
            showItems: this.data.cities
        });
    },
    inputFilter: function(t) {
        var a = {}, i = this.data.cities, s = t.detail.value.replace(/\s+/g, "");
        if (s.length) {
            for (var n in i) for (var r = i[n], l = 0, o = r.length; l < o; l++) {
                var u = r[l];
                -1 !== u.name.indexOf(s) && (e.isEmptyObject(a[n]) && (a[n] = []), a[n].push(u));
            }
            e.isEmptyObject(a) && (a = null), this.setData({
                alternative: a,
                showItems: a
            });
        } else this.setData({
            alternative: null,
            showItems: i
        });
    },
    getSortedAreaObj: function(t) {
        for (var e = {}, a = 0, i = (t = t.sort(function(t, e) {
            return t.letter > e.letter ? 1 : t.letter < e.letter ? -1 : 0;
        })).length; a < i; a++) {
            var s = t[a];
            delete s.districts;
            var n = s.letter;
            e[n] || (e[n] = []), e[n].push(s);
        }
        return e;
    },
    choose: function(t) {
        var e = t.currentTarget.dataset.name, a = getCurrentPages(), i = a[a.length - 2];
        e ? i.search(e, function() {
            wx.navigateBack({});
        }) : i.init({}, function() {
            wx.navigateBack({});
        });
    },
    getHotCities: function(t) {
        var e = this;
        wx.cloud.callFunction({
            name: "getHotCities",
            data: {}
        }).then(function(t) {
            var a = t.result.data;
            a && e.setData({
                hotCities: a
            });
        });
    },
    onLoad: function() {
        this.getHotCities();
        var e = this.getSortedAreaObj(t.cities || []);
        this.setData({
            cities: e,
            showItems: e
        });
    }
});